import { Component, ViewChild } from '@angular/core';

import { Storage } from '@ionic/storage';

import { ModalController, NavController, ActionSheetController, Content } from 'ionic-angular';

import { Config } from '../../../../providers/config';


import _ from 'lodash';
import {MarketingLinupListService} from "./list.service";
import {MarketingLineupDetailPage} from "../detail/detail.component";
import {MarketingLineupSavePage} from "../save/save.component";
import {MarketingLineupQueryPage} from "../query/query.component";


@Component({
  selector: 'page-marketing-lineup-list',
  templateUrl: './list.html',
  providers: [MarketingLinupListService]
})
export class MarketingLineupListPage {
	moa_page_name: any = "page-marketing-lineup-list";
  moa_page_explain: any = "任务管理";

  @ViewChild(Content) content: Content;

  // 列表数据
  listData: any = [];
  //显示标题
  title = '';

  _ = _;
  // 页码
  pageIndex = 1;
  // 是否加载更多
  isLoadMore = true;

  keyWord = "";
  //
  queryCondition = null;

  defaultEmpByTree = [];

  usr_id = "";

  constructor(
    public modalCtrl: ModalController,
    public navCtrl: NavController,
    public config: Config,
    public storage: Storage,
    public listService: MarketingLinupListService,
    public actionSheetCtrl: ActionSheetController
    ) {

    this.storage.remove("advancedQueryCondition");

      this.initQueryCondition();
      this.title = "未完成";
      this.queryCondition.taskstatus = "未完成";

      // 初始化数据
      this.loadListData(this.pageIndex, null, null);
  }


  /**
   * [loadListData 加载列表数据]
   * @param {[type]} page           [页码]
   * @param {[type]} infiniteScroll [加载更多组件]
   * @param {[type]} refresher      [下拉刷新组件]
   */
   loadListData(page, infiniteScroll, refresher) {
     console.log('this.queryCondition');
     console.log(this.queryCondition);
     this.listService.getListData(page, this.queryCondition).subscribe(data => {
       if (data) {
         console.info(data);
         // 如果加载第一页，则清空数据
         if (page == 1) {
           this.isLoadMore = true;
           this.listData = [];
         }
         // 把现有数据和新加载的数据合并
         this.listData = _.concat(this.listData, data);
         // 增加搜索索引
         this.pageIndex = this.pageIndex + 1;

         // 如果返回数据少于请求页数，则不再加载更多
         if (data.length < this.config.pageSize) {
           this.isLoadMore = false;
         }
       } else {
         if (this.pageIndex == 1) {
           this.listData = [];
         }
         // this.listData = [];
         this.isLoadMore = false;
       }
       // complete event
       if (infiniteScroll) {
         infiniteScroll.complete();
       }
       if (refresher) {
         refresher.complete();
       }
     })
   }
  /**
   * [goDetailPage 跳转详情页]
   * @param {[type]} item [参数]
   */
   goDetailPage(item) {
     console.log("----------");
     console.info(item);
       this.navCtrl.push(MarketingLineupDetailPage, {
         "detail" : item
       });
   }

   ionViewDidLoad() {
     console.log(new Date());
   }

   ionViewDidEnter() {
     this.storage.get("doRefresh").then(data => {
       if (data) {
         this.queryCondition.resourceid = "'ALL'";
         this.queryCondition.taskname = "";
         this.pageIndex = 1;
         this.loadListData(this.pageIndex, null, null);
         this.storage.remove("doRefresh");
       }
     })
   }

  /**
   * [doInfinite 加载更多]
   * @param {[type]} infiniteScroll [事件]
   */
   doInfinite(infiniteScroll) {
     if (this.pageIndex != 1) {
       console.info("-----------infiniteScroll------------");
       console.info(infiniteScroll);
       if (this.isLoadMore) {
         this.loadListData(this.pageIndex, infiniteScroll, null);
       } else {
         infiniteScroll.complete();
       }
     } else {
       infiniteScroll.complete();
     }
   }
  /**
   * [doRefresh 下拉刷新]
   * @param {[type]} refresher [事件]
   */
   doRefresh(refresher) {
     this.pageIndex = 1;
     this.isLoadMore = true;
     this.loadListData(this.pageIndex, null, refresher);
   }


   search() {
       this.queryCondition.resourceid = "'ALL'";
      this.queryCondition.taskname = this.keyWord;
      this.pageIndex = 1;
      this.loadListData(this.pageIndex, null, null);
   }

   showAdvancedQueryModal() {

     console.log("打开Modal");
     let queryModal = this.modalCtrl.create(MarketingLineupQueryPage, {

     });
     queryModal.onWillDismiss(data => {
       if (data && data != "close") {
         this.storage.get("advancedQueryCondition")
         .then(data => {
           if (data != null) {
             this.queryCondition = data;
             if (this.queryCondition.taskstatus == "完成") {
               this.title = "已完成"
             } else {
               if (this.queryCondition.taskstatus === "") {
                 this.title = "全部";
               } else {
                 this.title = "未完成";
               }
             }
             this.keyWord = "";
             this.pageIndex = 1;
             this.loadListData(this.pageIndex, null, null);
           }
         });
       }
     });
     queryModal.present();
   }

   initQueryCondition() {
     this.queryCondition = {
       "taskname":"",
       "taskstatus" : "",
       "resourceid": "'ALL'",
       "empName": ""
     };
   }

  chooseListType() {
    let bottons = [{
      text: '全部',
      handler: () => {
        this.title = "全部";
        this.queryCondition.taskstatus = "";
        this.pageIndex = 1;
        this.loadListData(this.pageIndex, null, null);
      }
    }, {
      text: '未完成',
      handler: () => {
        this.title = "未完成";
        this.queryCondition.taskstatus = "未完成";
        this.pageIndex = 1;
        this.loadListData(this.pageIndex, null, null);
      }
    }, {
      text: '已完成',
      handler: () => {
        this.title = "已完成";
        this.queryCondition.taskstatus = "完成";
        this.pageIndex = 1;
        this.loadListData(this.pageIndex, null, null);
      }
    }, {
      text: '取消',
      role: 'cancel',
      handler: () => {

      }
    }];

    let actionSheet = this.actionSheetCtrl.create({
      title: '',
      buttons: bottons
    });
    actionSheet.present();
  }

  moreOp() {
     let bottons = [{
       text: '新增任务',
       handler: () => {
          this.navCtrl.push(MarketingLineupSavePage);
       }
     }, {
       text: '高级查询',
       handler: () => {
         this.showAdvancedQueryModal();
       }
     }, {
       text: '取消',
       role: 'cancel',
       handler: () => {
       }
     }];

     let actionSheet = this.actionSheetCtrl.create({
       title: '',
       buttons: bottons
     });
     actionSheet.present();
   };


}


